<!--# layout("/common/layout.html",{"jsBase":"/js/system/promoteRecord/"}){ -->

<form class="layui-form layui-form-pane" id="addForm" lay-filter="editform">
    <input type="hidden" name="promoteCode" id="promoteCode">
    <input type="hidden" name="accessType" id="accessType" value="${accessType}">
    <input type="hidden" name="recordName" id="recordName">
    <div class="layui-row">
        <div class="layui-form-item">
            <div class="layui-inline">
                <!--#if(accessType==6){ -->
                <label class="layui-form-label">归属</label>
                <div class="layui-input-inline">
                    <layui:simpleDataSelect name="orgId" value="" data="${orgList}" id="orgId"
                                            attrId="id" attrName="name" laySearch="true" layFilter="orgId"/>
                </div>
                <!--#}-->
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">所属渠道</label>
                <div class="layui-input-inline">
                    <select class="select" name="channelCode" id="channelCode" selectValue="" promtion="请选择" lay-search lay-filter="channelCode">
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">推广码</label>
                <div class="layui-input-inline">
                <select  id="record" name="record" lay-search="true" lay-filter="record">
                    <option  value="">请选择</option>

                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">链接</label>

                <layui:simpleDataSelect id="link" name="link" value="" data="${linkList}"
                                        attrId="linkAddr" attrName="linkName"/>

            </div>
        </div>
    </div>
   <!-- <layui:submitButtons id="createButton" text="生成链接" />-->
    <button class="layui-btn layui-btn-danger" type="button" id="createButton">生成链接</button>
    <button class="layui-btn" type="button" id="createButton-cancel">取消</button>
    <button class="layui-btn layui-btn-normal" type="button" id="export">导出</button>
    <div id="tableDiv">
        <table class="layui-table">
            <thead >
            <tr >
                <th>名称</th>
                <th>链接</th>
                <th>二维码</th>
            </tr>
            </thead>
            <tbody id="tbody">
            </tbody>

        </table>

    </div>
</form>
<!--#} -->
<script>
    layui.define(['form', 'laydate', 'table', 'promoteRecordApi'], function (exports) {
        var form = layui.form;
        var promoteRecordApi = layui.promoteRecordApi;
        var index = layui.index;
        var accessType = "${accessType}";
        var recordList;
        $("#tableDiv").hide();
        var view = {
            init: function () {
                Lib.initGenrealForm($("#addForm"), form);
                this.initSubmit();
            },
            initSubmit: function () {
                $("#createButton").click(function () {
                    var linkAddr = "";
                    if(($("#link").val()==null||$("#link").val()=="")&&($("#record").val()==null||$("#record").val()=="")){
                        Common.info("请选择推广码或链接");
                        return ;
                    }
                    var arr = new Array();
                    $("#tableDiv").show();
                    $('#tbody').empty();
                    if($("#link").val()!=null&&$("#link").val()!=""&&$("#record").val()!=null&&$("#record").val()!=""){  //都选
                        var val = $("#link").val();
                        if(val.indexOf("?") != -1){
                            linkAddr =  $("#link").val()+"&"+$("#record").val();
                        }else{
                            linkAddr =  $("#link").val()+"?"+$("#record").val();
                        }
                        $("#tbody").append(" <tr>" +
                            "      <td>"+$("#record").find("option:selected").text()+"</td>" +
                            "      <td>"+linkAddr+"</td>" +
                            "      <td id='td"+i+"'></td>" +
                            "    </tr>");
                        $('#td'+i).qrcode(linkAddr);
                    }else if($("#link").val()!=null&&$("#link").val()!=""){ // 只选择了连接
                        arr = recordList;
                        var xqo = eval('(' + arr + ')');

                        for(var i in xqo){
                            var val = $("#link").val();
                            if(val.indexOf("?") != -1){
                                linkAddr =  $("#link").val()+"&"+"channel="+xqo[i].channelCode+"&comeFrom="+ xqo[i].promoteCode;
                            }else{
                                linkAddr =  $("#link").val()+"?"+"channel="+xqo[i].channelCode+"&comeFrom="+ xqo[i].promoteCode;
                            }

                            $("#tbody").append(" <tr>" +
                                "      <td>"+xqo[i].promoteName+"</td>" +
                                "      <td>"+linkAddr+"</td>" +
                                "      <td id='td"+i+"'></td>" +
                                "    </tr>");
                            $('#td'+i).qrcode(linkAddr);
                        }
                    }else if($("#record").val()!=null&&$("#record").val()!=""){ // 只选择了推广码
                        arr = '${linkListJson}';
                        var xqo = eval('(' + arr + ')');
                        for(var i in xqo){
                            var linkAddr1 = xqo[i].linkAddr;
                            if(val.indexOf("?") != -1){
                                linkAddr =  xqo[i].linkAddr+"&"+$("#record").val();
                            }else{
                                linkAddr =  xqo[i].linkAddr+"?"+$("#record").val();
                            }
                            $("#tbody").append(" <tr>" +
                                "      <td>"+xqo[i].linkName+"</td>" +
                                "      <td>"+linkAddr+"</td>" +
                                "      <td id='td"+i+"'></td>" +
                                "    </tr>");
                            $('#td'+i).qrcode(linkAddr);
                        }
                    }
                });

                $("#createButton-cancel").click(function () {
                    Lib.closeFrame();
                });

                $("#cpbutton").click(function(){
                    $("#promoteLink").select();
                    document.execCommand("Copy");
                    return false;
                })

                $("#export").click(function () {
                    if(($("#link").val()==null||$("#link").val()=="")&&($("#record").val()==null||$("#record").val()=="")){
                        Common.info("请选择推广码或链接");
                        return ;
                    }
                    var formPara = $("#addForm").serializeJson();
                    Common.openConfirm("确认要导出这些数据?", function() {
                        Common.post("/system/promoteRecord/export.json", formPara, function(fileId) {
                            Lib.download(fileId);
                        })
                    })
                });
            }

        }
        view.init();
        getChannel();
        getRecord();
        form.on('select(orgId)', function(data){
            getChannel();
            getRecord();
        });

        form.on('select(channelCode)', function(data){
            getRecord();
        });

        form.on('select(record)', function(data){
            $("#recordName").val($("#record").find("option:selected").text());
        });

        function getChannel() {
            var orgId = $("#orgId").val();
            $.ajax({
                type: 'POST',
                url: '${ctxPath}/system/promoteRecord/getChannel.json',
                data: {
                    orgId:orgId,
                    accessType:accessType
                },
                async: false,
                dataType:'json',
                success: function(data){

                    $("#channelCode").html("");
                    var option1 = $("<option>").val("").text("请选择");
                    $("#channelCode").append(option1);
                    $.each(data, function(key, val) {
                       if(val.channelCode!=null){
                           var option1 = $("<option>").val(val.channelCode).text(val.channelName);
                           $("#channelCode").append(option1);
                       }
                    });
                    form.render('select');
                    // getRecord();
                }
            });
        }

        function getRecord() {
            var channelCode = $("#channelCode").val();
            var orgId = $("#orgId").val();
            $.ajax({
                type: 'POST',
                url: '${ctxPath}/system/promoteRecord/getRecord.json',
                data: {
                    orgId:orgId,
                    accessType:accessType,
                    channelCode:channelCode
                },
                async: false,
                dataType:'json',
                success: function(data){
                    recordList = JSON.stringify(data);
                    $("#record").html("");
                    var option1 = $("<option>").val("").text("请选择");
                    $("#record").append(option1);
                    $.each(data, function(key, val) {
                        if(val.channelCode!=null){
                            var option1 = $("<option>").val("channel="+val.channelCode+"&comeFrom="+val.promoteCode).text(val.promoteName);
                            $("#record").append(option1);
                        }
                    });
                    form.render('select');

                }
            });
        }


    });
</script>
<script type="text/javascript" src="${ctxPath}/plugins/jquery/qrcode.js?v=${jsVer}"></script>
<script type="text/javascript" src="${ctxPath}/plugins/jquery/jquery.qrcode.js?v=${jsVer}"></script>
